package format;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * 
 */

/**
 * @author bocar
 *
 */
public class StatData {

	public void statArray(String[] strs) {
		Map<String, Integer> map = new HashMap<String, Integer>();
		for (int i = 0; i < strs.length; i++) {
			Integer count = map.get(strs[i]);
			map.put(strs[i], (count == null || count == 0) ? 1 : count + 1);
		}
		
//		for (Iterator it = map.keySet().iterator(); it.hasNext();) {
//			String currStr = (String) it.next();
//			System.out.println(currStr + " : " + map.get(currStr));
//		}
		
		for (String currStr : map.keySet()) {
			System.out.println(currStr + " : " + map.get(currStr));
		}
	}
	
	public void statUser(User[] users) {
		Map<User, Integer> map = new HashMap<User, Integer>();
		for (int i = 0; i < users.length; i++) {
			Integer count = map.get(users[i]);
			map.put(users[i], (count == null || count == 0) ? 1 : count + 1);
		}
		
		for (Iterator it = map.keySet().iterator(); it.hasNext();) {
			User user = (User) it.next();
			System.out.println(user + " : " + map.get(user));
		}
	}
	
	
	public <T> void stat(T[] array) {
		Map<T, Integer> map = new HashMap<T, Integer>();
		for (int i = 0; i < array.length; i++) {
			Integer count = map.get(array[i]);
			map.put(array[i], (count == null || count == 0) ? 1 : count + 1);
		}
		
		for (Iterator it = map.keySet().iterator(); it.hasNext();) {
			T object = (T) it.next();
			System.out.println(object + " : " + map.get(object));
		}
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		StatData test = new StatData();
		String[] strs = new String[]{"a","c","b","c","a","a","d"};
		test.stat(strs);
		
		User[] users = new User[] {
				new User("Jack",21,1),
				new User("Jack",21,1),
				new User("Jack",20,1),
				new User("Rose",21,0),
				new User("Rose",22,0),
				new User("Jack",23,1),
				new User("Rose",21,0),
				new User("Jack",21,1)
		};
		test.stat(users);
	}

}
